package cn.mickey.exchange.api;


import cn.mickey.exchange.annotation.LoginUser;
import cn.mickey.exchange.annotation.RedisCache;
import cn.mickey.exchange.domain.JsonResponse;
import cn.mickey.exchange.domain.model.FavoriteModel;
import cn.mickey.exchange.domain.model.UserModel;
import cn.mickey.exchange.service.FavoriteService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RedisCache
@RestController
@RequestMapping("/favorite")
public class FavoriteApi {

    @Resource
    private FavoriteService favoriteService;

    @PostMapping("/add")
    public JsonResponse addFavorite(@LoginUser UserModel userModel,
                                    @RequestBody FavoriteModel favoriteModel) {
        favoriteModel.setUserId(userModel.getUserId());
        if (favoriteService.addFavorite(favoriteModel)) {
            return JsonResponse.success(favoriteModel);
        }
        return JsonResponse.fail("已被收藏!");
    }

    @GetMapping("/delete")
    public JsonResponse deleteFavorite(@LoginUser UserModel userModel,
                                       @RequestParam Long id) {
        if (favoriteService.deleteFavorite(id)) {
            return JsonResponse.success("删除成功!");
        }
        return JsonResponse.fail("系统错误!");
    }

    @GetMapping("/check")
    public JsonResponse checkFavorite(@RequestParam Long userId,
                                      @RequestParam Long idleId) {
        return JsonResponse.success(favoriteService.isFavorite(userId, idleId));
    }

    @GetMapping("/my")
    public JsonResponse getMyFavorite(@LoginUser UserModel user) {
        return JsonResponse.success(favoriteService.getAllFavorite(user.getUserId()));
    }
}
